package vn.candyapp.qlct.fragment;

import java.util.Date;
import vn.candyapp.qlct.R;
import vn.candyapp.qlct.adapter.DsGiaoDichAdapter;
import vn.candyapp.qlct.database.DBAdapter;
import vn.candyapp.qlct.database.model.Transaction;
import vn.candyapp.qlct.item.KieuTKGiaoDich;
import vn.candyapp.qlct.utils.DialogUtils;
import vn.candyapp.qlct.utils.DialogUtils.IDialogConfirmListener;
import vn.candyapp.widget.ActionItem;
import vn.candyapp.widget.QuickAction;
import vn.candyapp.widget.QuickAction.OnActionItemClickListener;
import android.app.ProgressDialog;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

public class DSGiaoDichFragment extends BaseFragment {
	
	private static final String KEY_CONTENT = "DSGiaoDichFragment:Content";
	public static final int ACTION_DELETE = 0x01;

	public static DSGiaoDichFragment newInstance(Date date, KieuTKGiaoDich type) {
		DSGiaoDichFragment f = new DSGiaoDichFragment();
		f.mDate = date;
		f.mType = type;
		return f;
	}
	
	private Date mDate;
	private KieuTKGiaoDich mType;
	private ListView mListView;
	private DsGiaoDichAdapter mAdapter;
	private TextView tvNoTran;
	private QuickAction quickAction;
	private int mItemClicked = 0;
	private DBAdapter dbAdapter;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		dbAdapter = new DBAdapter(getActivity());
		if (savedInstanceState != null && savedInstanceState.containsKey(KEY_CONTENT)) {
			Bundle args = savedInstanceState.getBundle(KEY_CONTENT);
			mDate = new Date(args.getLong("DATE"));
			mType = (KieuTKGiaoDich) args.getSerializable("TYPE");
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.dsgiaodich_fragment, container, false);
		mListView = (ListView) view.findViewById(R.id.lvGiaoDich);
		tvNoTran = (TextView) view.findViewById(R.id.tvHasNoTran);
		getActivity().registerForContextMenu(mListView);
		mAdapter = new DsGiaoDichAdapter(getActivity());
		mAdapter.loadData(new Runnable() {
			
			@Override
			public void run() {
				mListView.setAdapter(mAdapter);
				if (mAdapter.getCount() == 0) {
					tvNoTran.setVisibility(View.VISIBLE);
				}
			}
		}, mDate, mType);
		quickAction = new QuickAction(getActivity(), QuickAction.VERTICAL);
		ActionItem actionDelete = new ActionItem(ACTION_DELETE, getString(R.string.delete));
		quickAction.addActionItem(actionDelete);
		quickAction.setOnActionItemClickListener(new OnActionItemClickListener() {
			
			@Override
			public void onItemClick(QuickAction source, int pos, int actionId) {
				if (actionId == ACTION_DELETE) {
					DialogUtils.showConfirm(getActivity(), "Bạn có muốn xóa giao dịch này ?", new IDialogConfirmListener() {

						@Override
						public void onOk() {
							Transaction tran = (Transaction) mAdapter.getItem(mItemClicked);
							dbAdapter.deleteTransaction(tran);
							reloadDsGiaoDichAdapter();
						}

						@Override
						public void onCancel() {
						}
						
					});
				}
			}
		});
		
		mListView.setOnItemClickListener(new ListView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				mItemClicked = position;
				quickAction.show(view);
			}
		});
		return view;
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		Bundle args = new Bundle();
		args.putLong("DATE", mDate.getTime());
		args.putSerializable("TYPE", mType);
		outState.putBundle(KEY_CONTENT, args);
		super.onSaveInstanceState(outState);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
//		switch (item.getItemId()) {
//			case R.id.menu_edit:
//				break;
//			case R.id.menu_delete:
//				break;
//		}
		return super.onContextItemSelected(item);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
//		getActivity().getMenuInflater().inflate(R.menu.dsgiaodich, menu);
	}
	
	private void reloadDsGiaoDichAdapter() {
		mAdapter.loadData(new Runnable() {
			
			@Override
			public void run() {
				mAdapter.notifyDataSetChanged();
				if (mAdapter.getCount() == 0) {
					tvNoTran.setVisibility(View.VISIBLE);
				}
			}
		}, mDate, mType);
	}
}
